/*
 * @Author: 张小平
 * @Date:   2025-05-01 
 * @Email:  qqshuqian@163.com 
 * @Copyright © 2024 aseyun. All rights reserved.
 */
import _ from 'lodash'
import { nodeType,suffix } from '@/framework-code/utils/constants'

const genId = () => {
  let id = (new Date()).getTime()
  let randomNumber = Math.floor(Math.random() * 100000)
  return `${id}${randomNumber}${suffix}`
}

const toArray = (tree, arr = []) => {
  const item = {}
  _.forEach(tree, function(val, key) {
    if (key === 'child') {
      toArray(val, arr)
    } else if (key === 'conditions') {
      if (val && val.length) {
        for (let i = 0; i < val.length; i++) {
          toArray(val[i], arr)
        }
      }
    } else {
      item[key] = val
    }
  })
  if (Object.keys(item).length) {
    arr.push(item)
  }
  return arr
}

const arrayDropById = (arr, id) => {
  const index = _.findIndex(arr, { id })
  arr.splice(index, 1)
  return arr
}

const arrayAddById = (arr, id, item) => {
  const index = _.findIndex(arr, { id })
  arr.splice(index, 0, item)
  return arr
}

const arrayEditById = (arr, id, item) => {
  const index = _.findIndex(arr, { id })
  arr.splice(index, 1, item)
  return arr
}

const toTree = (arr, pid = null, obj = {}) => {
  const children = arr.filter(item => item.pid === pid)

  const childLen = children.length

  if (childLen) {
    if (childLen === 1) {
      const item = children[0]
      obj = {
        ...item
      }

      const obj1 = toTree(arr, item.id)

      const iterateObj = (obj1) => {
        if (Object.keys(obj1).length) {
          if (Array.isArray(obj1)) {
            // branch
            obj.conditions = []
            obj.child = {}
            _.each(obj1, (obj1Item) => {
              const obj2 = toTree(arr, obj1Item.id)
              if (Object.keys(obj2).length && !Array.isArray(obj2)) {
                obj1Item.child = obj2
              } else {
                if (obj1Item.type === nodeType.ROUTE) {
                  obj1Item.conditions = obj2
                }
              }

              if (obj1Item.type === nodeType.BRANCHTASK) {
                obj.conditions.push(obj1Item)
              } else {
                obj.child = {
                  ...obj1Item
                }
              }
            })
          } else {
            // node
            obj.child = obj1
          }
        }
      }

      iterateObj(obj1)
    } else if (childLen > 1) {
      return children
    }
  }

  return obj
}

const arrayFindBranchs = (arr, pid) => {
  return arr.filter(item => {
    return item.pid === pid && item.type === nodeType.BRANCHTASK
  })
}

const arrayFindBranchChild = (arr, pid, routePid) => {
  return arr.filter(item => {
    if (item.pid === pid) {
      item.pid = routePid
    }
    return item
  })
}

const arrayFindRoute = (arr, id) => {
  return arr.filter(item => item.id === id && item.type === nodeType.ROUTE)
}

const arrayFindChild = (arr, id) => {
  return arr.filter(item => item.pid === id)
}

const arrayLastChild = (arr, id) => {
  let child = arr.filter(item => item.pid === id)
  if (child.length) {
    let child1 = arrayLastChild(arr, child[0].id)
    if (child1.length) {
      child = child1
    }
  }
  return child
}

const arrayRouteChild = (arr, routeId) => {
  return arr.filter(item => item.pid === routeId && item.type !== nodeType.BRANCHTASK)
}

const arraySetPid = (arr, id, pid) => {
  return _.map(arr, (item) => {
    if (item.id === id) {
      item.pid = pid
    }
    return item
  })
}

const dealWithPid = (arr, id, pid) => {
  return _.map(arr, (item) => {
    if (item.pid === id) {
      item.pid = pid
    }
    return item
  })
  // console.log(arr, id, pid)
}

export default {
  genId,
  toArray,
  arrayDropById,
  arrayAddById,
  arrayEditById,
  arrayFindBranchs,
  arrayFindRoute,
  arrayFindBranchChild,
  arrayFindChild,
  arrayLastChild,
  arrayRouteChild,
  arraySetPid,
  dealWithPid,
  toTree
}
